package com.ruoyi.common.core.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.ruoyi.common.core.serializer.LocalDateDeserializer;
import com.ruoyi.common.core.serializer.LocalDateSerializer;
import com.ruoyi.common.core.serializer.LocalDateTimeDeserializer;
import com.ruoyi.common.core.serializer.LocalDateTimeSerializer;
import com.ruoyi.common.core.serializer.LocalTimeDeserializer;
import com.ruoyi.common.core.serializer.LocalTimeSerializer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 * 作用：处理 Jackson 配置
 * </p>
 *
 * @author zhanglianbao
 * @since 2025-02-08 13:57
 */
@Configuration
public class JacksonConfig {


  @Bean
  public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    // 创建 SimpleModule 并注册自定义反序列化器
    SimpleModule module = new SimpleModule();
    module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
    module.addDeserializer(LocalDate.class, new LocalDateDeserializer());
    module.addDeserializer(LocalTime.class, new LocalTimeDeserializer());
    module.addSerializer(Long.class, ToStringSerializer.instance);
    module.addSerializer(Long.TYPE, ToStringSerializer.instance);
    module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
    module.addSerializer(LocalDate.class, new LocalDateSerializer());
    module.addSerializer(LocalTime.class, new LocalTimeSerializer());

    // 注册模块到 ObjectMapper
    mapper.registerModule(module);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
  }
}
